package com.liu.security.controller;

import org.springframework.http.HttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * Created by shi860715@126.com
 * Date 2021/8/12 14:35
 * 获取当前登录用户
 */
@RestController
public class UserController {






    @GetMapping("/user1")
    public Object user1(Principal principal){
        return  principal;

    }

    @GetMapping("/user2")
    public Object user2(Authentication authentication){
        return  authentication;

    }

    @GetMapping("/user3")
    public Object user3(HttpServletRequest request){
        return  request.getUserPrincipal();

    }

    @GetMapping("/user4")
    public Object user4(){
        return SecurityContextHolder.getContext().getAuthentication();

    }





}
